package com.example.asapp.demo.curd;

import com.alibaba.excel.EasyExcel;
import com.example.asapp.annotation.HasRole;
import com.example.asapp.util.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/api/demo/curd")
public class CurdApi {

    @Autowired
    CurdService curdService;

    @GetMapping("listQuery")
    public List<Curd> findAll(){
        return curdService.findAll();
    }

    @PostMapping("save")
    @HasRole({"admin","user"})
    public Curd saveOne(@RequestBody Curd curd){
        return curdService.saveOne(curd);
    }

    @DeleteMapping("delete/{id}")
    public void deleteOne(@PathVariable Integer id){
       curdService.deleteByPrimary(id);
    }


    @PostMapping("download")
    public void download(HttpServletResponse response) throws Exception {
        EasyExcel.write(ExcelUtil.getOutputStream("curdlist",response), Curd.class).sheet("sheet1")
                .doWrite(curdService.findAll());

    }

}
